package swing;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import container.Weapon;
import container.WeaponTraitDescriptions;
import enums.FieldNames;

/**
 * Frame to create and edit {@link Weapon}s.
 * 
 * @author Marcus
 * 
 */
public class WeaponCreationFrame extends JFrame implements ActionListener {

	/** Generated serial ID	 */
	private static final long serialVersionUID = -8049946156904132158L;
	private WeaponTraitDescriptions weaponTraits;
	private Weapon changedWeapon;
	
	public static final String OK = "OK";
	public static final String CANCEL = "Cancel";
	
	private JTextField weaponNameField;
	private JTextField weaponCodeField;
	private JTextField accuracyField;
	private JTextField optimalRangeField;
	private JTextField longRangeField;
	private JTextField weaponCostField;
	private JTextField damageField;
	

	public void setWeaponTraits(WeaponTraitDescriptions traits) {
		this.weaponTraits = traits;
	}
	
	private JComponent createTextFieldComponent() {
		// Create the text fields
		this.weaponNameField = new JTextField(20);
		this.weaponCodeField = new JTextField(6);
		this.accuracyField = new JTextField(3);
		this.optimalRangeField = new JTextField(6);
		this.longRangeField = new JTextField(6);
		this.damageField = new JTextField(6);
		this.weaponCostField = new JTextField(10);

		// Create the Labels for the text fields
		JLabel weaponNameFieldLabel = new JLabel(FieldNames.WEAPON_NAME
				.getFieldName());
		weaponNameFieldLabel.setLabelFor(this.weaponNameField);
		
		JLabel weaponCodeFieldLabel = new JLabel(FieldNames.WEAPON_CODE
				.getFieldName());
		weaponCodeFieldLabel.setLabelFor(this.weaponCodeField);
		
		JLabel accuracyFieldLabel = new JLabel(
				FieldNames.WEAPON_ACCURACY.getFieldName());
		accuracyFieldLabel.setLabelFor(this.accuracyField);
		
		JLabel damageFieldLabel = new JLabel(
				FieldNames.WEAPON_DAMAGE.getFieldName());
		damageFieldLabel.setLabelFor(this.damageField);
		
		JLabel optimalFieldLabel = new JLabel(
				FieldNames.WEAPON_OPTIMAL_RANGE.getFieldName());
		optimalFieldLabel.setLabelFor(this.optimalRangeField);
		
		JLabel longFieldLabel = new JLabel(
				FieldNames.WEAPON_LONG_RANGE.getFieldName());
		longFieldLabel.setLabelFor(this.longRangeField);
		
		JLabel costFieldLabel = new JLabel(
				FieldNames.WEAPON_COST.getFieldName());
		costFieldLabel.setLabelFor(this.weaponCostField);

		// Lay out the text controls and the labels.
		JPanel textControlsPane = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		textControlsPane.setLayout(gridbag);

		JLabel[] labels = { weaponNameFieldLabel, weaponCodeFieldLabel,
				accuracyFieldLabel, damageFieldLabel, optimalFieldLabel, longFieldLabel, costFieldLabel };
		JTextField[] textFields = { this.weaponNameField, this.weaponCodeField,
				this.accuracyField,this.damageField,this.optimalRangeField,this.longRangeField,this.weaponCostField };
		addLabelTextRows(labels, textFields, gridbag, textControlsPane);

		c.gridwidth = GridBagConstraints.REMAINDER; // last
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 1.0;
		textControlsPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Weapon Trait"), BorderFactory
						.createEmptyBorder(50, 50, 50, 50)));
		
		return textControlsPane;
	}
	
	private void addLabelTextRows(JLabel[] labels, JTextField[] textFields,
			GridBagLayout gridbag, Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		int numLabels = labels.length;

		for (int i = 0; i < numLabels; i++) {
			c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
			c.fill = GridBagConstraints.NONE; // reset to default
			c.weightx = 0.0; // reset to default
			container.add(labels[i], c);

			c.gridwidth = GridBagConstraints.REMAINDER; // end row
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			container.add(textFields[i], c);
		}
	}	
	
	/**
	 * Designes the Frame with all its components.
	 */
	private void designFrame() {
		JComponent weaponInfoComponent = new JComponent();
		weaponInfoComponent.repaint();
		JComponent descriptionComponent = createDescriptionArea();
		descriptionComponent.repaint();
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
				weaponInfoComponent, descriptionComponent);

		splitPane.setVisible(true);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(150);

		// Provide minimum sizes for the two components in the split pane
		Dimension minimumSize = new Dimension(500, 200);
		weaponInfoComponent.setMinimumSize(minimumSize);
		descriptionComponent.setMinimumSize(minimumSize);

		add(splitPane, BorderLayout.CENTER);
		add(createButtonPanel(), BorderLayout.PAGE_END);

		pack();
		setVisible(false);
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

}
